import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
import SubCategory from '@/views/SubCategory/index.vue'
import Detail from '@/views/Detail/index.vue'
import CartList from '@/views/CartList/index.vue'
import Checkout from '@/views/Checkout/index.vue'
import Pay from '@/views/Pay/index.vue'
import PayBack from '@/views/Pay/PayBack.vue'
import Member from '@/views/Member/index.vue'
import UserInfo from '@/views/Member/components/UserInfo.vue'
import UserOrder from '@/views/Member/components/UserOrder.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      component: Layout,
      children: [
        { path: '', component: Home }, // 这里路径值为空，可以让浏览器访问'/'时默认访问该页面(默认二级路由设置) 也可以通过redirect重定向做到
        { path: 'category/:id', component: Category },
        { path: 'category/sub/:id', component: SubCategory },
        { path: 'detail/:id', component: Detail },
        { path: '/cartlist', component: CartList },
        { path: '/checkout', component: Checkout },
        { path: '/pay', component: Pay },
        { path: '/paycallback', component: PayBack },
        {
          path: '/member',
          component: Member,
          redirect: '/member/user',
          children: [
            { path: '/member/user', component: UserInfo },
            { path: '/member/order', component: UserOrder }
          ]
        }
      ]
    }
  ],
  // 路由滚动行为定制，路由变化自定回到页顶
  scrollBehavior () {
    return {
      top: 0
    }
  }
})

export default router

// import { createRouter, createWebHistory } from 'vue-router'

// const router = createRouter({
//   history: createWebHistory('/'),
//   routes: [
//     { path: '/', component: Layout }
//   ],
//   scrollBehavior () {
//     return {
//       top: 0
//     }
//   }
// })

// router.beforeEach((to, from) => {
//   if (to.path !== '/login' && !Author.token ) {
//     return '/login'
//   }
// })

// export default router